/**
 * 
 */
package com.glodon.paas.account.dao.redis;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.Transaction;

import com.glodon.paas.account.dao.domain.OAuthTokenEntity;

/**
 * @author hankl
 *
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value = {
		"classpath:spring-config-test.xml",
		"classpath:paas-account-dao-mysql.xml"		
		})
public class OAuthTokenDaoRedisImplIT {

    @Autowired
    private JedisPool pool;
    
    @Autowired	
    private OAuthTokenEntity token;
    
    @Autowired
    private OAuthTokenDaoRedisImpl redisTokenDao;
	
    private String key = "736da21e-270f-40fc-9205-6c2e4310e2b5"; //token
	protected static Map<Integer, String> userMap = new HashMap<Integer, String>();
    /**
	 * @throws java.lang.Exception
	 */
	@Before
	public void setUp() throws Exception {		
		//token.setAccessToken(false); //test request token
		//token.setoAuthAuthorizationId("");//test request token
		redisTokenDao.saveToken(token); //test access token
	}
	
	public static void initUser() throws IOException {
//		BufferedReader reader = new BufferedReader(new FileReader(new File(
//				"D:/test/data/uid.csv")));
//		String userId = "";
//		int i = 0;
//		while ((userId = reader.readLine()) != null) {
//			userId = userId.substring(1, userId.length() - 2);
//			userMap.put(i++, userId);
//		}
		}
	@Test
	@Ignore
	public void  Save()throws Exception{
//		initUser();
//		
//		for(int i=0; i<100000;i++){
//			
//		String tokenValue="token_"+i;
//		String uid=userMap.get(i);
//		OAuthTokenEntity accessToken = new OAuthTokenEntity();
//		accessToken.setAccessToken(true);
//		accessToken.setAuthorized(true);
//		accessToken.setConsumerKey(token.getConsumerKey());
//		accessToken.setUserId(uid);
//		accessToken.setCallbackUrl(token.getCallbackUrl());
//		accessToken.setSecret("secret");
//		accessToken.setValue(tokenValue);
//		accessToken.setTimestamp(System.currentTimeMillis());
//			
//		redisTokenDao.saveToken(accessToken);
//		}
		

	}
	@Test
	@Ignore
	public void  SaveKeyValue()throws Exception {
		final Jedis jedis  = pool.getResource();
		 Transaction transaction = jedis.multi();
		 for(int i=10000;i<100000;i++)
			 transaction.set("test_"+i,"testValue");
		
		 transaction.exec();
		 
		 pool.returnResource(jedis);
	}
	
	
	@Test
	@Ignore
	public void  Del()throws Exception {
        final Jedis jedis = pool.getResource();
        for (String key : jedis.keys("oauth:access:token:" + "*"+":token_"+"*")) {
           jedis.del(key);
        }
        pool.returnResource(jedis);
	}

	/**
	 * @throws java.lang.Exception
	 */
	@After
	public void tearDown() throws Exception {
        final Jedis jedis = pool.getResource();
        for (String key : jedis.keys(OAuthTokenDaoRedisImpl.DEFAULT_OAUTH_PREFIX + "*"+"736da21e-270f-40fc-9205-6c2e4310e2b5")) {
           jedis.del(key);
        }
        pool.returnResource(jedis);
	}
	/**
	 * Test method for {@link com.glodon.paas.account.dao.redis.OAuthTokenDaoRedisImpl#readToken(java.lang.String)}.
	 */
	@Test
	public void  ReadToken() {	
		OAuthTokenEntity t = redisTokenDao.readToken(token.getValue());
		assertNotNull(t);
		assertThat(t.getValue(),is(key));
		assertTrue(t.isAccessToken());
	}

	/**
	 * Test method for {@link com.glodon.paas.account.dao.redis.OAuthTokenDaoRedisImpl#saveToken(com.glodon.paas.account.dao.domain.OAuthTokenEntity)}.
	 */
	@Test
	public void  SaveToken() {			
		OAuthTokenEntity t = redisTokenDao.readToken(token.getValue());
		assertNotNull(t);      
        assertThat(t.getValue(), is(key));
        assertTrue(t.isAccessToken());
	}

	/**
	 * Test method for {@link com.glodon.paas.account.dao.redis.OAuthTokenDaoRedisImpl#removeToken(java.lang.String)}.
	 */
	@Test
	public void  RemoveTokenString() {
		OAuthTokenEntity t1 = redisTokenDao.readToken(token.getValue());
		OAuthTokenEntity t2 = redisTokenDao.removeToken(token.getValue());
		assertThat(t1.getValue(),is(t2.getValue()));		
		assertNull(redisTokenDao.readToken(token.getValue()));
	}

	/**
	 * Test method for {@link com.glodon.paas.account.dao.redis.OAuthTokenDaoRedisImpl#removeToken(java.lang.String, java.lang.String)}.
	 */
	@Test	
	public void  RemoveTokenStringString() {
		OAuthTokenEntity t = redisTokenDao.readToken(token.getValue());
		redisTokenDao.removeToken(token.getConsumerKey(),token.getUserId());
		assertNull(redisTokenDao.readToken(token.getValue()));
		assertThat(t.getValue(),is(token.getValue()));
	}
	
	/**
	 * Test method for {@link com.glodon.paas.account.dao.redis.OAuthTokenDaoRedisImpl#insert(com.glodon.paas.account.dao.domain.OAuthTokenEntity)}.
	 */
	@Test
	public void  Insert() {		
		OAuthTokenEntity t = redisTokenDao.readToken(token.getValue());
		assertNotNull(t);      
        assertThat(t.getValue(), is(key));
        assertTrue(t.isAccessToken());
	}

	/**
	 * Test method for {@link com.glodon.paas.account.dao.redis.OAuthTokenDaoRedisImpl#get(java.lang.String)}.
	 */
	@Test
	public void  Get() {
		OAuthTokenEntity t = redisTokenDao.readToken(token.getValue());
		assertNotNull(t);
		assertThat(t.getValue(),is(key));
		assertTrue(t.isAccessToken());
	}

	/**
	 * Test method for {@link com.glodon.paas.account.dao.redis.OAuthTokenDaoRedisImpl#delete(java.lang.String)}.
	 */
	@Test
	public void  Delete() {
		redisTokenDao.delete(token.getValue());			
		assertNull(redisTokenDao.readToken(token.getValue()));
	}

}
